#coding:utf-8
from lovewith.settings import PAGE_SIZE
from lovewith.utils.mtcache import MtCache
from lovewith.utils.tools import my_iterator
from lovewith.album.models import Album, AlbumAttach
from lovewith.share.views.modules.attach_flag import AttachFlagApi


class AlbumApi:
    def __init__(self):
        pass

    #校验album数据
    @staticmethod
    def check_data(album_id, user_id):
        if album_id:
            try:
                album_id = int(album_id)
                album_data = Album.objects.filter(id=album_id, user_id=user_id)
                if album_data.exists():
                    return album_data[0]
            except ValueError:
                pass
        return None

    #获取总数
    @staticmethod
    def get_total():
        album_total = MtCache('site_album_total').get()
        if not album_total:
            album_total = Album.objects.all().count()
            MtCache('site_album_total').set(album_total)

        return album_total

    #获取用户全部灵感板
    @staticmethod
    def get_album_by_user(user_id):
        return Album.objects.filter(user_id=user_id).order_by('-id')

    #获取用户灵感板数量
    @staticmethod
    def get_album_count_by_user(user_id):
        return Album.objects.filter(user_id=user_id).count()

    #添加一个灵感板
    @staticmethod
    def add(album_name, user):
        new_album = Album(
            name=album_name,
            user=user
        )
        new_album.save()
        return new_album.id

    #更新名称
    @staticmethod
    def update(new_name, album_data):
        album_data.name = new_name
        album_data.save()

    #灵感板中前3张封面图片
    @staticmethod
    def get_cover(album_id):
        return AlbumAttach.objects.filter(album_id=album_id).order_by('-id')[:3]

    #灵感板图片总数
    @staticmethod
    def get_image_count_by_album(album_id):
        return AlbumAttach.objects.filter(album_id=album_id).count()

    #加入图片被加入到哪些灵感板中
    @staticmethod
    def get_added_album(image_id, user_id):
        album_data = []
        album_filter_data = AlbumAttach.objects.filter(post_image_id=image_id, user_id=user_id)
        for d in album_filter_data:
            album_data.append(d.album_id)
        return album_data

    #添加图片到灵感板
    def add_to_album(self, image_data, album_ids, user):
        new_album_attach = []
        for album_id in album_ids:
            #判断灵感板权限
            album_data = self.check_data(int(album_id), user.id)
            if album_data:
                #判断是否已添加
                has_added_album = self.get_added_album(image_data.id, user.id)
                if len(has_added_album) == 0:
                    new_album_attach.append(AlbumAttach(post_image=image_data, album=album_data, user=user))

            #更新缓存
            if album_id:
                MtCache('user_album_share').delete(album_id=album_id, page=1)

        if len(new_album_attach) > 0:
            AlbumAttach.objects.bulk_create(new_album_attach)

    #从灵感板中删除一张图片
    @staticmethod
    def delete_image_from_album(image_data):
        album_id = None
        image_filter_data = AlbumAttach.objects.filter(post_image_id=image_data.id)
        if image_filter_data:
            album_id = image_filter_data[0].album_id
            image_filter_data.delete()

        #更新缓存
        if album_id:
            MtCache('user_album_share').delete(album_id=album_id, page=1)

    #删除灵感板
    @staticmethod
    def delete(album_data):
        #删除灵感板收藏的灵感
        album_image_data = AlbumAttach.objects.filter(album_id=album_data.id)
        album_image_data.delete()

        #删除灵感板
        album_data.delete()

    #根据album_id获取图片
    def get_share_by_album(self, album_id, page_no):
        album_share_data = MtCache('user_album_share').get(album_id=album_id, page=page_no)
        if not album_share_data:
            share_data = []
            start = (page_no - 1) * PAGE_SIZE
            last = page_no * PAGE_SIZE
            total = self.get_image_count_by_album(album_id)
            if last > total:
                last = total

            album_filter_data = AlbumAttach.objects.filter(post_image__share_post__is_delete=False, album_id=int(album_id)).order_by('-id')[start: last]
            for img in my_iterator(album_filter_data):
                #tk数据
                img_sale_data = AttachFlagApi.get_goods_by_image(img.post_image.id)
                if img_sale_data:
                    img_sale_data = img_sale_data[0]
                else:
                    img_sale_data = None

                share_data.append({
                    'title': img.post_image.share_post.title,
                    'uid': img.post_image.share_post.user.id,
                    'name': img.post_image.share_post.user.username,
                    'avatar': img.post_image.share_post.user.avatar,
                    'cover': img.post_image.path,
                    'share_id': img.post_image.share_post.id,
                    'cover_id': img.post_image.id,
                    'sale_data': img_sale_data
                })

            album_share_data = {'share': share_data, 'total': total}
            MtCache('user_album_share').set(album_share_data, album_id=album_id, page=page_no)

        return album_share_data